<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

//前台可拓展共用方法
abstract class MY_Controller extends CI_Controller {
    var $memcache = null;
    //定义枚举常量
    public function __construct() {
        parent::__construct ();
        $this->load->helper(array('url', 'form'));
        $this->load->helper('frame');
        date_default_timezone_set('Asia/Shanghai');
    }

    /**
     * 过滤评论
     */
    public function filer_chars($str)
    {
    	$str=preg_replace("/\s+/", " ", $str); //过滤多余回车
    
    	$str=preg_replace("/<[ ]+/si","<",$str); //过滤<__("<"号后面带空格)
    
    	$str=preg_replace("/<\!--.*?-->/si","",$str); //注释
    
    	$str=preg_replace("/<(\!.*?)>/si","",$str); //过滤DOCTYPE
    
    	$str=preg_replace("/<(\/?html.*?)>/si","",$str); //过滤html标签
    
    	$str=preg_replace("/<(\/?head.*?)>/si","",$str); //过滤head标签
    
    	$str=preg_replace("/<(\/?meta.*?)>/si","",$str); //过滤meta标签
    
    	$str=preg_replace("/<(\/?body.*?)>/si","",$str); //过滤body标签
    
    	$str=preg_replace("/<(\/?link.*?)>/si","",$str); //过滤link标签
    
    	$str=preg_replace("/<(\/?form.*?)>/si","",$str); //过滤form标签
    
    	$str=preg_replace("/cookie/si","COOKIE",$str); //过滤COOKIE标签
    
    	$str=preg_replace("/<(applet.*?)>(.*?)<(\/applet.*?)>/si","",$str); //过滤applet标签
    
    	$str=preg_replace("/<(\/?applet.*?)>/si","",$str); //过滤applet标签
    
    	$str=preg_replace("/<(style.*?)>(.*?)<(\/style.*?)>/si","",$str); //过滤style标签
    
    	$str=preg_replace("/<(\/?style.*?)>/si","",$str); //过滤style标签
    
    	$str=preg_replace("/<(title.*?)>(.*?)<(\/title.*?)>/si","",$str); //过滤title标签
    
    	$str=preg_replace("/<(\/?title.*?)>/si","",$str); //过滤title标签
    
    	$str=preg_replace("/<(object.*?)>(.*?)<(\/object.*?)>/si","",$str); //过滤object标签
    
    	$str=preg_replace("/<(\/?objec.*?)>/si","",$str); //过滤object标签
    
    	$str=preg_replace("/<(noframes.*?)>(.*?)<(\/noframes.*?)>/si","",$str); //过滤noframes标签
    
    	$str=preg_replace("/<(\/?noframes.*?)>/si","",$str); //过滤noframes标签
    
    	$str=preg_replace("/<(i?frame.*?)>(.*?)<(\/i?frame.*?)>/si","",$str); //过滤frame标签
    
    	$str=preg_replace("/<(\/?i?frame.*?)>/si","",$str); //过滤frame标签
    
    	$str=preg_replace("/<(script.*?)>(.*?)<(\/script.*?)>/si","",$str); //过滤script标签
    
    	$str=preg_replace("/<(\/?script.*?)>/si","",$str); //过滤script标签
    
    	$str=preg_replace("/javascript/si","Javascript",$str); //过滤script标签
    
    	$str=preg_replace("/vbscript/si","Vbscript",$str); //过滤script标签
    
    	$str=preg_replace("/on([a-z]+)\s*=/si","On\\1=",$str); //过滤script标签
    
    	$str=preg_replace("/&#/si","&＃",$str); //过滤script标签，如javAsCript:alert
    
    	return  strip_tags($str,"<blockquote></blockquote>");
    
    }
}
    /**
     * 前台封装类
     */
    class Front_Controller extends MY_Controller {
        /**
         * 跳转到指定页面
         */
        public function redirect_to($url, $msg = '') {
            $msg && $this->session->set_flashdata ( 'error', '<font color="red" size="5">' . $msg . '</font>' );
            redirect ( $url );
        }
        /**
         * GET请求适配器
         */
        public function _g($param) {
            return $this->input->get ( $param );
        }
        /**
         * POST请求适配器
         */
        public function _p($param) {
            return $this->input->post ( $param );
        }
        /**
         * 跳到上一页 并传送信息
         * @param String $msg
         */
        public function goto_prepage($msg) {
            $pre_page = $_SERVER ['HTTP_REFERER'];
            $this->redirect_to ( $pre_page, $msg );
        }
        /**
         *
         * @param Int $statu
         *        	响应状态
         * @param String $msg
         *        	响应消息
         */
        public function response_rs($statu, $msg) {
            exit ( json_encode ( array (
                "msg" => $msg,
                "statu" => $statu
            )));
        }
        
        /**
         * 前台视图
         * @param String $view_file
         * @param Array  $data
         * @param Array  $header
         */
        public function front_layout($view_file,$data=array())
        {
            $this->load->view ( $view_path . "/" . "header",$data);
            $this->load->view ( $view_path . "/" . $view_file, $data);
            $this->load->view ( $view_path . "/" . "footer");
        }
    }


//后台控制器扩展，如果您不了解其作用，请不要随意更改此处
    abstract class Admin_Controller extends MY_Controller
    {
        var $data= '';
        function __construct()
        {
            parent::__construct();
            $this->load->library('session');
            $this->load->library('form_validation');

            $this->_check_login();
        }

        function _check_login()
        {
            if(! $this->session->userdata('name'))
            {
                redirect('admin/login');
            }
        }
    }
    /**
     * 后台封装类
     * @author xiahuiqin
     *
     */
    abstract class Backend_Controller extends Front_Controller
    {
        //后台菜单列表-格式参考:reg_menu()
        var $menu = array();
        /**
         * @return the $menu
         */
        public function getMenu() {
            return $this->menu;
        }

        /**
         * @param multitype: $menu
         */
        public function setMenu($menu) {
            $this->menu = $menu;
        }

        public function __construct() {
            parent::__construct ();
            $this->login_();
            $this->load->database();
            $this->load->helper('url');
            //加载CRUD扩展类  实例化对象不做引用 权当引入类库
            $this->load->library('grocery_CRUD');
            //加载缩略图扩展类 实例化对象不做引用 权当引入类库
            $this->load->library('Thumbnails_');
        }
        /**
         * 后台管理视图layout
         *
         * @param String $view_file
         */
        public function admin_layout($view_file, $data = array()) {
            $view_path = "admin/backend";
            //获取后台菜单数据
            $menu_data = !empty($this->menu) ? $this->menu : $this->reg_menu();
            $this->load->view ( $view_path . "/" . "headerV",$data);
            $this->load->view ( 'admin/public/menuV');
            $this->load->view ( $view_path . "/" . $view_file, $data );
            $this->load->view ( $view_path . "/" . "footerV" ,$data);
        }
        /**
         * 注册菜单选项  供参考格式
         * 设置了属性menu此值不做返回
         */
        public function reg_menu()
        {
            return array("menu"=>array(
                array("title"=>"焦点图","url"=>"/orz/focus/"),
                array("title"=>"新闻","url"=>"/orz/news/"),
                array("title"=>"虎扑之队","url"=>"/orz/huputeam/"),
                array("title"=>"报表下载设置","url"=>"/orz/setting/"),
            ));
        }
        /**
         * 检查是否登录并未超级管理员
         */
        function login_()
        {
            if(! $this->session->userdata('name'))
            {
                redirect('admin/login');
            }
        }
        /**
         * 利用通用格式进行后台视图打印
         */
        public function backend_common_template($output)
        {
            $view_file = "output_template";
            $this->admin_layout($view_file,$output);
        }
}

